using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ltf.Remoting.RemoteEvents;
using Microsoft.TeamFoundation.VersionControl.Client;
using Ltf.Remoting.TeamFoundation;
using System.IO;
using Ltf.MonitorLizard.Helpers;
using Ltf.Remoting.Operations;

namespace Ltf.MonitorLizard.Forms
{
	[FormType(FormTypes.CheckOutLock)]
	public partial class CheckOutLock : Form
	{
		public CheckOutLock()
		{
			InitializeComponent();
		}

		private string[] pathList;

		public CheckOutLock(string[] pathList, LizardEventArgs args)
			: this()
		{
			this.pathList = pathList;
			LoadList(pathList);
		}

		private void LoadList(string[] pathList)
		{
			TFServerConnection connection = ServerConnections.GetConnectionForPath(pathList[0]);
			lvwItems.Items.Clear();
			lvwItems.SmallImageList = imlIcons;
	//		lvwItems.StateImageList = imlIcons;
			FileIcons fileIcons = new FileIcons(imlIcons);
			Workspace ws = connection.GetWorkspace(pathList[0]);
			foreach (string path in pathList)
			{
				string name = Path.GetFileName(path);
				string folder = Path.GetDirectoryName(ws.GetServerItemForLocalItem(path));
				ListViewItem lvi = new ListViewItem(new string[] { name, folder });
				if (Directory.Exists(path))
					lvi.ImageKey = "$folder";
				else
					lvi.ImageKey = fileIcons.LoadIconForFileExtension(Path.GetExtension(path));
				lvi.Checked = true;
				lvi.Tag = path;
				lvwItems.Items.Add(lvi);
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnCheckOutLock_Click(object sender, EventArgs e)
		{
			LockLevel lockLevel=LockLevel.None;


			if (rbtCheckInLock.Checked)
				lockLevel = LockLevel.Checkin;
			else if (rbtCheckOutLock.Checked)
				lockLevel = LockLevel.CheckOut;
				
			List<string> paths=new List<string>();
			foreach (ListViewItem lvi in lvwItems.Items)
			{
				if (lvi.Checked)
				{
					paths.Add(lvi.Tag.ToString());
				}
			}
			if (paths.Count > 0)
			{
				TFServerConnection connection = ServerConnections.GetConnectionForPath(paths[0]);

				Workspace ws = connection.GetWorkspace(paths[0]);
				WorkspaceOperators wso = new WorkspaceOperators(connection, ws);
				wso.PendEdit(paths.ToArray(), lockLevel);
			}

			this.Close();

		}

	}
}